package uplb.ics.cs190.e2d.lang;

public class GSystem {

	public static void verbose(String string) {
		System.out.println(string);
	}

	/**
	 * Invoke system garbage collection.
	 */
	public static void collect() {
		System.gc();
	}

	public static boolean isVersionValid(String minimum) {
		boolean valid = false;
		
		if(GSystem.isJ2SE()) {
			valid = GSystem.checkJ2SEVersion(minimum);
		} else if(GSystem.isJ2ME()) {
			valid = GSystem.checkJ2MEVersion(minimum);
		} else if(GSystem.isAndroid()) {
			valid = GSystem.checkAndroidVersion(minimum);
		}
		
		return valid;
	}
	
	private static boolean checkAndroidVersion(String minimum) {
		// TODO Auto-generated method stub
		return false;
	}

	private static boolean checkJ2MEVersion(String minimum) {
		String version = null;
		String profile = null;
		
		try {
			version = System.getProperty("microedition.configuration");
			profile = System.getProperty("microedition.profiles");
			
			// check for version compatibility
			if(version.startsWith("CLDC")) {
				version = version.substring(version.indexOf("-"));
				
			}
			
			// check for profile compatibility
			if(profile.startsWith("MIDP")) {
				profile = version.substring(version.indexOf("-"));
			}
		} catch(Exception e) {
			
		}
		
		return false;
	}

	private static boolean checkJ2SEVersion(String minimum) {
		String version = null;
		
		try {
			version = System.getProperty("java.version");
			
			char ver1, ver2, ver3;
			String update = null;
			
			ver1 = ver2 = ver3 = 'x';
			
			try {
				ver1 = version.charAt(0);
				ver2 = version.charAt(2);
				ver3 = version.charAt(4);
				update = version.substring(version.indexOf("_"));
			} catch(Exception e) {
				
			}
			
			if(ver1 == 'x' || ver2 == 'x') return false;
			
			// 1.6.0_24
			// ^this
			if(ver1 != '1') return false;
			
			// 1.6.0_24
			//   ^this
			try {
				Integer.parseInt(Character.toString(ver2));
			} catch(Exception e) {
				return false;
			}
			
			// 1.6.0_24
			//     ^this
			if(ver3 != 'x') {
				try {
					Integer.parseInt(Character.toString(ver3));
				} catch(Exception e) {
					return false;
				}
			}
			
			// 1.6.0_24
			//		 ^this
			if(update != null) {
				try {
					Integer.parseInt(update);
				} catch(Exception e) {
					return false;
				}
			}
			
			/* COMPARE TO MINIMUM VERSION SPECIFIED */
		} catch(Exception e) {
			return false;
		}
		
		return true;
	}

	protected static boolean isJ2ME() {
		boolean j2me;
		
		try {
			System.getProperty("microedition.configuration");
			j2me = true;
		} catch(Exception e) {
			j2me = false;
		}
		
		return j2me;
	}
	
	protected static boolean isJ2SE() {
		boolean j2se;
		
		try {
			String vm = System.getProperty("java.vm.name");
			j2se = false;
			if(vm.compareTo("Java Hotspot(TM) Client VM") == 0) {
				j2se = true; 
			}
		} catch(Exception e) {
			j2se = false;
		}
		
		return j2se;
	}
	
	protected static boolean isAndroid() {
		boolean android;
		
		try {
			String vm = System.getProperty("java.vm.name");
			android = false;
			if(vm.compareTo("Dalvik") == 0) {
				android = true;
			}
		} catch(Exception e) {
			android = false;
		}
		
		return android;
	}
}
